|
mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
|
mrbgems is a library manager to integrate C and Ruby extensions in an easy and standardised way into mruby. Conventionally, each mrbgem name is prefixed by mruby-, e.g. mruby-time for a gem that provides Time class functionality.
You have to activate mrbgems explicitly in your build configuration. To add a gem, add the following line to your build configuration file, for example:
You can also use a relative path to specify a gem.
In that case,
A remote GIT repository location for a GEM is also supported:
NOTE: :bitbucket option supports only git. Hg is unsupported in this version.
You can specify the subdirectory of the repository with :path option:
To use mrbgem from mgem-list use :mgem option:
For specifying the commit hash to checkout use :checksum_hash option:
If there are missing dependencies, mrbgem dependencies solver will reference mrbgem from the core or mgem-list.
Note that if more than one git-based gem has the same base name (i.e. the default checkout directory name), it is (now) an error UNLESS they have the same repository URL, branch name and commit-id (i.e. checksum hash). You can bypass this by explicitly importing your preferred version first and setting the canonical: option to true:
If you do this, the system will (mostly) silently ignore other attempts to clone a gem with this name.
Note that this only affects cloning the gem from git. It does not resolve version conflicts. If the version as specified in the gem's rakefile is incompatible with a dependency, your build will still fail.
You can give blocks in the conf.gem call to make adjustments for environments where the original gem does not expect them:
However, it should be used with caution, as it may deviate from the intent of the gem's author.
If you enable unit tests in your build with enable_test, tests will be generated for all gems and their dependencies by default. If necessary, it is possible to suppress tests for a specific gem like so:
However, it is considered best practice to leave all tests enabled whenever possible. A warning message will be generated for each gem with disabled tests.
There are instances when you wish to add a collection of mrbgems into mruby at once, or be able to substitute mrbgems based on configuration, without having to add each gem to your build configuration file. A packaged collection of mrbgems is called a GemBox. A GemBox is a file that contains a list of mrbgems to load into mruby, in the same format as if you were adding them to the build config via config.gem, but wrapped in an MRuby::GemBox object. GemBoxes are loaded into mruby via config.gembox 'boxname'.
Below we have created a GemBox containing mruby-time and mrbgems-example:
As mentioned, the GemBox uses the same conventions as MRuby::Build. The GemBox must be saved with a .gembox extension inside the mrbgems directory to be picked up by mruby.
To use this example GemBox, we save it as custom.gembox inside the mrbgems directory in mruby, and add the following to your build configuration file inside the build block:
This will cause the custom GemBox to be read in during the build process, adding mruby-time and mrbgems-example to the build.
If you want, you can put GemBox outside the mruby directory. In that case you must specify an absolute path like below.
There are two GemBoxes that ship with mruby: default and full-core. The default GemBox contains several core components of mruby, and full-core contains every gem found in the mrbgems directory.
The maximal GEM structure looks like this:
The mrblib directory contains pure Ruby files to extend mruby. The src directory contains C/C++ files to extend mruby. The include directory contains C/C++ header files. The test directory contains C/C++ and pure Ruby files for testing purposes which will be used by mrbtest. mrbgem.rake contains the specification to compile C and Ruby files. README.md is a short description of your GEM.
mrbgems expects a specification file called mrbgem.rake inside of your GEM directory. A typical GEM specification could look like this for example:
The mrbgems build process will use this specification to compile Object and Ruby files. The compilation results will be added to lib/libmruby.a. This file exposes the GEM functionality to tools like mruby and mirb.
The following properties can be set inside your MRuby::Gem::Specification for information purpose:
The license and author properties are required in every GEM!
In case your GEM is depending on other GEMs please use spec.add_dependency(gem, *requirements[, default_get_info]) like:
The version requirements and default gem information are optional.
Version requirement supports following operators:
When more than one version requirements is passed, the dependency must satisfy all of it.
You can have default gem to use as dependency when it's not defined in your build configuration. When the last argument of add_dependency call is Hash, it will be treated as default gem information. Its format is same as argument of method MRuby::Build#gem, except that it can't be treated as path gem location.
When a special version of dependency is required, use MRuby::Build#gem in the build configuration to override default gem.
If you have conflicting GEMs use the following method:
like following code:
In case your GEM has more complex build requirements you can use the following options additionally inside your GEM specification:
You also can use spec.mruby.cc and spec.mruby.linker to add extra global parameters for the compiler and linker.
Your GEM can export include paths to another GEMs that depends on your GEM. By default, /...absolute path.../{GEM_NAME}/include will be exported. So it is recommended not to put GEM's local header files on include/.
These exports are transitive. For example: when B depends on C and A depends on B, A will get include paths exported by C.
Exported include_paths are automatically appended to GEM local include_paths by rake. You can use spec.export_include_paths accessor if you want more complex build.
When the block argument passed to MRuby::Gem::Specification.new is executed, the GEM build commands/tasks for the MRuby::Build instance may not yet be finalized. In most cases, modifying the GEM build commands/tasks within the block passed to MRuby::Gem::Specification.new is not a problem.
However, you may need to perform GEM build commands/tasks after the GEM build commands/tasks for the MRuby::Build instance have been finalized. In such cases, you can achieve this by passing a block argument to MRuby::Gem::Specification#build_settings within the block passed to MRuby::Gem::Specification.new.
NOTE: Using the build_settings method will cause GEM's all build command settings directly written in the block passed to MRuby::Gem::Specification.new to be ignored.
mruby can be extended with C. This is possible by using the C API to integrate C libraries into mruby.
mrbgems expects that you have implemented a C method called mrb_YOURGEMNAME_gem_init(mrb_state). YOURGEMNAME will be replaced by the name of your GEM. If you call your GEM c_extension_example, your initialisation method could look like this:
mrbgems expects that you have implemented a C method called mrb_YOURGEMNAME_gem_final(mrb_state). YOURGEMNAME will be replaced by the name of your GEM. If you call your GEM c_extension_example, your finalizer method could look like this:
mruby can be extended with pure Ruby. It is possible to override existing classes or add new ones in this way. Put all Ruby files into the mrblib directory.
none
mruby can be extended with C and Ruby at the same time. It is possible to override existing classes or add new ones in this way. Put all Ruby files into the mrblib directory and all C files into the src directory.
mruby codes under mrblib directory would be executed after gem init C function is called. Make sure mruby script depends on C code and C code doesn't depend on mruby script.
See C and Ruby example.
Some gems can generate executables under bin directory. Those gems are called binary gems. Names of binary gems are conventionally prefixed by mruby-bin, e.g. mruby-bin-mirb and mruby-bin-strip.
To specify the name of executable, you need to specify spec.bins in the mrbgem.rake. The entry point main() should be in the C source file under tools/<bin>/*.c where <bin> is a name of the executable. C files under the <bin> directory are compiled and linked to the executable, but not included in libmruby.a, whereas files under mrblib and src are.
It is strongly recommended not to include mrblib and src directories in the binary gems, to separate normal gems and binary gems.